$(function(){

    $('#survey_submit').click(function(){
        $("#survey_form").validate();
        if(!$("#survey_form").valid( )){
            return;
        }

        var results = [];
        var surveyInfo = $('#questions').metadata();
        $('li.question').each(function(){
            var rs = {
                surveyId: surveyInfo.surveyId,
                pageIndex: surveyInfo.pageIndex,
                questionId: $(this).metadata().questionId,
                questionType:  $(this).metadata().questionType,
                itemIdStr: ''
            };

            var questionType =  $(this).metadata().questionType;
            if(questionType == 'radio' || questionType == 'checkbox'){
                $(this).find('.options input:checked').each(function(){
                    rs.itemIdStr += $(this).val() + ',';
                });
                rs.itemIdStr = rs.itemIdStr.slice(0, -1);
                results.push(rs);
            }else if(questionType== 'radio_matrix' || questionType == 'checkbox_matrix'){
                $(this).find('tr.sub_question').each(function(){
                    var sub_question_rs = {
                        surveyId: surveyInfo.surveyId,
                        pageIndex: surveyInfo.pageIndex,
                        questionId: $(this).metadata().questionId,
                        questionType:  rs.questionType,
                        parentId: $(this).metadata().parentId,
                        itemIdStr: ''
                    };

                    $(this).find('input:checked').each(function(){
                        sub_question_rs.itemIdStr += $(this).val() + ',';
                    });

                    sub_question_rs.itemIdStr = sub_question_rs.itemIdStr.slice(0, -1);
                    results.push(sub_question_rs);
                });
            }else if(questionType == 'select'){
                $(this).find('select option:selected').each(function(){
                    rs.itemIdStr += $(this).val() + ',';
                });
                rs.itemIdStr = rs.itemIdStr.slice(0, -1);
                results.push(rs);
            }else if(questionType == 'multi_input'){
                rs.itemIdStr += $(this).find('textarea').val();
                results.push(rs);
            }
        });
//        if(console) console.log(JSON.stringify(results));

        var url = 'survey?data=:data&surveyId=:surveyId&title=:title&pageIndex=:pageIndex&surveySN=:surveySN&ptId=:ptId&sndaId=:sndaId';
        url = url.replace(":data", encodeURIComponent(JSON.stringify(results)))
            .replace(":surveyId", encodeURIComponent(surveyInfo.surveyId))
            .replace(":title", encodeURIComponent(surveyInfo.title))
            .replace(":pageIndex", encodeURIComponent(surveyInfo.pageIndex))
            .replace(":surveySN", encodeURIComponent($('#surveySN').val()))
            .replace(":ptId", encodeURIComponent($('#ptId').val()))
            .replace(":sndaId", encodeURIComponent($('#sndaId').val()));

        if(console) console.log(url);

        $('#survey_form').attr('action', url);
        $('#survey_form').submit();

//        $.post('survey', {
//            data: JSON.stringify(results),
//            surveyId: surveyInfo.surveyId,
//            title: surveyInfo.title,
//            pageIndex: surveyInfo.pageIndex,
//            surveySN: $('#surveySN').val(),
//            ptId: $('#ptId').val(),
//            sndaId: $('#sndaId').val()
//        });
    });
});